﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace _4SquareLite
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
            Program.arrForm.Add(this);
        }

        private void inputPanel1_EnabledChanged(object sender, EventArgs e)
        {
            if (inputPanel1.Enabled)
            {
                pnlForm.Height -= inputPanel1.Bounds.Height;
            }
            else
            {
                pnlForm.Height = this.Height;
            }
        }

        private void miExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void miLogin_Click(object sender, EventArgs e)
        {
            Login();
        }

        private void Login()
        {
            string strUsername = tbUsername.Text.Trim();
            string strPassword = tbPassword.Text.Trim();

            if ((!String.IsNullOrEmpty(strUsername)) && (!String.IsNullOrEmpty(strPassword)))
            {
                string strAuthenticat = strUsername + ":" + strPassword;
                strAuthenticat = Convert.ToBase64String(Encoding.Default.GetBytes(strAuthenticat));

                LocalSettings.Save("Authentication", strAuthenticat);

                Cursor.Current = Cursors.WaitCursor;
                string strResult = Program.HttpGet("http://api.foursquare.com/v1/user");
                Cursor.Current = Cursors.Default;

                if (String.IsNullOrEmpty(strResult))
                {
                    MessageBox.Show("Oops! You've entered either an incorrect email, phone number or password.", Program.strProgramName,
                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                }
                else
                {
                    frmDashboard oForm = new frmDashboard(strResult);
                    oForm.Show();
                    this.Hide();
                }
            }
        }
    }
}